package com.jic.market.impl.rest.discount.processor;

import com.jic.market.common.util.Money;
import com.jic.market.entity.DiscountOrderDetail;
import com.jic.market.entity.PromotionRuleEntity;
import com.jic.market.enums.DiscountAmountServiceOperateEnum;
import com.jic.market.impl.rest.discount.model.ConditionDiscountModel;
import com.jic.market.impl.rest.discount.model.GoodsDetailModel;
import com.jic.market.mapper.DiscountOrderDetailMapper;
import com.jic.market.mapper.PromotionRuleEntityMapper;
import com.jic.market.request.rest.discount.DiscountBaseRequest;
import com.jic.market.request.rest.discount.DiscountCancelRequest;
import com.jic.market.request.rest.discount.DiscountOrderPayRequest;
import com.jic.market.request.rest.discount.DiscountQueryRequest;
import com.jic.market.response.rest.discount.GiftDetail;
import com.jic.market.service.rest.discount.GiftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 满额-赠
 * @author yinhaijun
 * @date: 2020/5/7
 */
@Service("GiftFullQuotaProcessor")
public class GiftFullQuotaProcessor extends AbstractConditionDiscountProcessor {
    @Autowired
    private PromotionRuleEntityMapper promotionRuleEntityMapper;
    @Autowired
    private GiftService giftService;
    @Autowired
    private DiscountOrderDetailMapper discountOrderDetailMapper;
    @Override
    public ConditionResult isCondition(ConditionDiscountModel conditionDiscountModel, Map<String, GoodsDetailModel> goodsDetailModelMap, Object request, DiscountAmountServiceOperateEnum operate) {
        if(!checkPromotion(((DiscountBaseRequest)request).getThirdUserId(), ((DiscountBaseRequest)request).getAdminOperate().getChildId().toString(), conditionDiscountModel.getChannelCodes(), conditionDiscountModel.getPromotionMemberGradeList())){
            return new ConditionResult(false, "满额赠计算"+conditionDiscountModel.getPromotionCode()+"会员等级校验未通过");
        }
        // 总额计算
        BigDecimal actualTotalAmount = conditionDiscountModel.getActualTotalAmount(goodsDetailModelMap);

        // 超出购买按原价处理
        List<PromotionRuleEntity> promotionRuleEntityList = promotionRuleEntityMapper.queryByPromotionId(Long.parseLong(conditionDiscountModel.getPromotionId()));
        PromotionRuleEntity promotionRuleEntity = getFlightPromotionRuleEntity(actualTotalAmount, promotionRuleEntityList);
        // 不优惠处理
        if(null == promotionRuleEntity){
            return new ConditionResult(false, "满额赠无优惠");
        }
        // 最终匹配成功需要乘的倍数
        Integer overlayAppNum = getBeiSu(conditionDiscountModel, actualTotalAmount, promotionRuleEntity);

        // 查询出所有的赠送商品
        List<GiftDetail> giftDetailList = giftService.getGiftDetails(conditionDiscountModel.getPromotionId(), conditionDiscountModel.getPromotionCode(), ((DiscountBaseRequest) request).getThirdUserId(), promotionRuleEntity, overlayAppNum.longValue());

        if(!giftDetailList.isEmpty()){
            ConditionResult conditionResult = new ConditionResult(true, "有优惠");
            conditionResult.setObject(giftDetailList);
            return conditionResult;
        }
        return null;
    }

    @Override
    public void queryShoppingCartAfter(ConditionResult conditionResult, ConditionDiscountModel conditionDiscountModel, Map<String, GoodsDetailModel> goodsDetailModelMap, Object request){
        List<GiftDetail> giftDetailList = (List<GiftDetail>) conditionResult.getObject();
        conditionDiscountModel.setGiftDetailList(giftDetailList);
    }

    @Override
    public void querySettlementAfter(ConditionResult conditionResult, ConditionDiscountModel conditionDiscountModel, Map<String, GoodsDetailModel> goodsDetailModelMap, Object request){
        List<GiftDetail> giftDetailList = (List<GiftDetail>) conditionResult.getObject();
        DiscountQueryRequest discountQueryRequest = (DiscountQueryRequest) request;
        Map<String, List<GiftDetail>> giftMap = discountQueryRequest.getGiftMap();
        if(null != giftMap && !giftMap.isEmpty()){
            List<GiftDetail> giftDetailList1 = giftService.getMixedGiftDetails(giftMap.get(conditionDiscountModel.getPromotionCode()), giftDetailList);
            conditionDiscountModel.setGiftDetailList(giftDetailList1);
        }
    }


    @Override
    public void createAfter(ConditionDiscountModel conditionDiscountModel, Map<String, GoodsDetailModel> goodsDetailModelMap, Object request){


    }

    @Override
    public void payAfter(ConditionDiscountModel conditionDiscountModel, Map<String, GoodsDetailModel> goodsDetailModelMap, Object request){
        List<GiftDetail> giftDetailList = conditionDiscountModel.getGiftDetailList();
        DiscountOrderDetail discountOrderDetailQ = new DiscountOrderDetail();
        discountOrderDetailQ.setOrderNo(((DiscountOrderPayRequest) request).getOrderNo());
        List<DiscountOrderDetail> discountOrderDetailList =  discountOrderDetailMapper.query(discountOrderDetailQ);
        giftService.issuanceGift(giftDetailList, discountOrderDetailList.get(0).getThirdUserId());
    }

    @Override
    public void isAllCancelAfter(ConditionDiscountModel conditionDiscountModel, Map<String, GoodsDetailModel> goodsDetailModelMap, Object request) {
        List<GiftDetail> giftDetailList = conditionDiscountModel.getGiftDetailList();
        DiscountOrderDetail discountOrderDetailQ = new DiscountOrderDetail();
        discountOrderDetailQ.setOrderNo(((DiscountCancelRequest) request).getOrderNo());
        List<DiscountOrderDetail> discountOrderDetailList =  discountOrderDetailMapper.query(discountOrderDetailQ);
        giftService.recycleGift(giftDetailList, discountOrderDetailList.get(0).getThirdUserId());
    }

    protected PromotionRuleEntity getFlightPromotionRuleEntity(BigDecimal condition, List<PromotionRuleEntity> promotionRuleEntityList) {
        if(null != promotionRuleEntityList && !promotionRuleEntityList.isEmpty()){
            PromotionRuleEntity promotionRuleEntity = null;
            Integer level = -1;
            for(int i = 0; i<promotionRuleEntityList.size();i++){
                Long conditionValue = promotionRuleEntityList.get(i).getConditionValue();
                if(promotionRuleEntityList.get(i).getLevel() > level && condition.compareTo(new BigDecimal(conditionValue/100)) != -1){
                    promotionRuleEntity = promotionRuleEntityList.get(i);
                    level = promotionRuleEntityList.get(i).getLevel();
                }
            }
            return promotionRuleEntity;

        }
        return null;
    }

    private Integer getBeiSu(ConditionDiscountModel promotionAmountModel, BigDecimal condition, PromotionRuleEntity promotionRuleEntity) {
        Integer overlayAppNum = 1;
        if(promotionAmountModel.getOverlayAppFlag()){
            BigDecimal divideInt = Money.divideInt(condition, Money.divide(new BigDecimal(promotionRuleEntity.getConditionValue()), new BigDecimal(100)));
            if(divideInt.compareTo(new BigDecimal(promotionAmountModel.getOverlayAppNum())) == 1){
                overlayAppNum = promotionAmountModel.getOverlayAppNum();
            }else {
                overlayAppNum = divideInt.intValue();
            }
        }
        return overlayAppNum;
    }

}
